package tieba.web;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import tieba.core.CookieUtils;
import tieba.core.TBApi;
import tieba.core.WebUtils;
import tieba.entity.CmsUser;
import tieba.entity.TbSignTask;
import tieba.entity.TbUser;
import tieba.service.ICmsUserService;
import tieba.service.ITbSignTaskService;
import tieba.service.ITbUserService;
import tieba.sign.execute.SignAction;
import cl.tools.http.method.BasicMethod;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Resource
	private ICmsUserService cmsUserService;

	@Resource
	private ITbUserService tbUserService;

	@Resource
	private ITbSignTaskService tbSignTaskService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String menu(HttpServletRequest req, ModelMap map) {
		CmsUser user = null;
		if ((user = CookieUtils.isLogin(req, cmsUserService)) == null)
			return "redirect:/index";
		map.addAttribute("user", user);
		map.addAttribute("active_num", 1);
		map.addAttribute("item_page", "my-index");
		return "menu/index";
	}

	@RequestMapping(value = "/online_sign/{id}", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.GET)
	public @ResponseBody String onlineSign(HttpServletRequest req,
			@PathVariable("id") int id, ModelMap map) {
		String body = BasicMethod.get(
				TBApi.CORE + "/like/" + tbUserService.findById(id).getBduss())
				.asString();
		return body;
	}

	@RequestMapping(value = "/online_sign", method = RequestMethod.GET, params = "id")
	public String onlineSign(HttpServletRequest req, @RequestParam("id") int id) {
		// 此处判断贴吧账号是否关联CMS账号
		return "menu/online_sign";
	}

	@RequestMapping(value = "/timing-sign", method = RequestMethod.GET)
	public String timingSign(HttpServletRequest req, ModelMap map) {
		CmsUser cu = CookieUtils.isLogin(req, cmsUserService);
		if (cu != null) {
			map.addAttribute("active_num", 4);
			map.addAttribute("item_page", "timing-sign");
			map.addAttribute("bd_users", cu.getTbUsers());
			map.addAttribute("sign_tasks",cu.getTbSignTasks());
			return "menu/index";
		}
		return "redirect:/index";
	}

	@RequestMapping(value = "/task/add/{id}", method = RequestMethod.GET)
	public String taskAdd(@PathVariable("id") int id, HttpServletRequest req,
			ModelMap map) {
		TbUser tu = tbUserService.findById(id);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 2);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		tbSignTaskService.save(new TbSignTask(tu, CookieUtils.isLogin(req,
				cmsUserService), tu.getUsername(), calendar.getTime(), 0, 0));
		return WebUtils.isLogin("redirect:/menu/timing-sign", req,
				cmsUserService);
	}

	@RequestMapping(value = "/task/del/{id}", method = RequestMethod.GET)
	public String taskDel(@PathVariable("id") int id, HttpServletRequest req,
			ModelMap map) {
		tbSignTaskService.remove(tbSignTaskService.findById(id));
		return WebUtils.isLogin("redirect:/menu/timing-sign", req,
				cmsUserService);
	}

	@RequestMapping(value = "/task/exe/{id}", method = RequestMethod.GET)
	public String taskExe(@PathVariable("id") int id, HttpServletRequest req,
			ModelMap map) {
		new SignAction(tbSignTaskService.findById(id), tbSignTaskService)
				.execute();
		return WebUtils.isLogin("redirect:/menu/timing-sign", req,
				cmsUserService);
	}

}
